#include <iostream>
#include <condition_variable>
#include <mutex>
#include <thread>
using namespace std;

mutex mtx;
condition_variable cond;
int flag = 1;

void pA() 
{
    unique_lock<mutex> lk(mtx);
    int count = 10;
    while (count >= 0) 
    {
        cond.wait(lk, [] {return flag == 1; });
        cout << "t1 -- A" << endl;
        flag = 2;
        count--;
        cond.notify_all();
    }
}

void pB()
{
    unique_lock<mutex> lk(mtx);
    int count = 10;
    while (count >= 0)
    {
        cond.wait(lk, [] {return flag == 2; });
        cout << "t2 -- B" << endl;
        flag = 3;
        count--;
        cond.notify_all();
    }
}

void pC()
{
    unique_lock<mutex> lk(mtx);
    int count = 10;
    while (count >= 0)
    {
        cond.wait(lk, [] {return flag == 3; });
        cout << "t3 -- C" << endl;
        flag = 1;
        count--;
        cond.notify_all();
    }
}

int main()
{
    thread t1(pA);
    thread t2(pB);
    thread t3(pC);

    t1.join();
    t2.join();
    t3.join();

    return 0;
}